#! /bin/bash
# enconding: utf-8

set -e

cd "$(dirname $0)"
source common.sh

app_version="$(get_app_version)"
target_archive="Squirrel-${app_version}.zip"
download_url="https://github.com/rime/squirrel/releases/download/${app_version}/${target_archive}"

verify_archive() {
    if ! [ -f "${target_archive}" ]; then
        echo >&2 "ERROR: file does not exit: ${target_archive}"
        exit 1
    fi
    echo 'sha256 checksum:'
    echo "${checksum}  ${target_archive}"
    shasum -a 256 -c <<<"${checksum}  ${target_archive}" || exit 1
}

create_archive() {
    if [ -e "${target_archive}" ]; then
        echo >&2 "ERROR: target archive already exists: ${target_archive}"
        exit 1
    fi
    zip -r "${target_archive}" Squirrel.pkg
    echo 'sha256 checksum:'
    shasum -a 256 "${target_archive}"
}

if [ -n "${checksum}" ]; then
    verify_archive
else
    create_archive
fi

# sign_key: the private key file for signing the zip archive in Sparkle appcast.
# usage: checksum='...' sign_key='sign/dsa_priv.pem' ./make_archive
if [ -z "${sign_key}" ]; then
    echo 'sign_key unspecified; skip signing.'
    exit
fi

dsa_signature=$(ruby sign/sign_update.rb "${target_archive}" "${sign_key}")
file_length=$(stat -f%z "${target_archive}")
pub_date=$(date -R)

# for release channel
appcast_file='appcast.xml'

cat > "${appcast_file}" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
  <channel>
    <title>【鼠鬚管】輸入法更新頻道</title>
    <link>https://rime.github.io/release/squirrel/appcast.xml</link>
    <description>鼠鬚管 Appcast 更新頻道</description>
    <language>zh</language>
    <item>
      <title>鼠鬚管 ${app_version}</title>
      <sparkle:releaseNotesLink>https://rime.github.io/release/squirrel/</sparkle:releaseNotesLink>
      <sparkle:minimumSystemVersion>10.9.0</sparkle:minimumSystemVersion>
      <pubDate>${pub_date}</pubDate>
      <enclosure url="${download_url}"
                 sparkle:version="${app_version}"
                 sparkle:dsaSignature="${dsa_signature}"
                 length="${file_length}"
                 type="application/octet-stream"/>
    </item>
  </channel>
</rss>
EOF

ls -l ${appcast_file}

# for testing channel
appcast_file='testing-appcast.xml'

cat > "${appcast_file}" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
  <channel>
    <title>【鼠鬚管】輸入法測試頻道</title>
    <link>https://rime.github.io/testing/squirrel/appcast.xml</link>
    <description>鼠鬚管測試版 Appcast 更新頻道</description>
    <language>zh</language>
    <item>
      <title>鼠鬚管 ${app_version}</title>
      <sparkle:releaseNotesLink>https://rime.github.io/testing/squirrel/</sparkle:releaseNotesLink>
      <sparkle:minimumSystemVersion>10.9.0</sparkle:minimumSystemVersion>
      <pubDate>${pub_date}</pubDate>
      <enclosure url="${download_url}"
                 sparkle:version="${app_version}"
                 sparkle:dsaSignature="${dsa_signature}"
                 length="${file_length}"
                 type="application/octet-stream"/>
    </item>
  </channel>
</rss>
EOF

ls -l ${appcast_file}

# for testing appcast push locally.
appcast_file='debug-appcast.xml'
download_url="file://${PROJECT_ROOT}/package/${target_archive}"

cat > "${appcast_file}" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle">
  <channel>
    <title>【鼠鬚管】輸入法本地測試</title>
    <link>file://${PROJECT_ROOT}/package/${appcast_file}</link>
    <description>鼠鬚管 Appcast 更新本地測試</description>
    <language>zh</language>
    <item>
      <title>鼠鬚管 ${app_version}</title>
      <sparkle:releaseNotesLink>https://rime.github.io/testing/squirrel/</sparkle:releaseNotesLink>
      <sparkle:minimumSystemVersion>10.9.0</sparkle:minimumSystemVersion>
      <pubDate>${pub_date}</pubDate>
      <enclosure url="${download_url}"
                 sparkle:version="${app_version}"
                 sparkle:dsaSignature="${dsa_signature}"
                 length="${file_length}"
                 type="application/octet-stream"/>
    </item>
  </channel>
</rss>
EOF

ls -l ${appcast_file}
